// 文本格式化
var ckeditExtensionOb = ckeditExtensionOb || {};
(function() {
    ckeditExtensionOb.ckEditorHandle = (editor)=>{
        let editData = editor.getData();
        editData = editData.replace(/(^\s*)|(\s*$)/g, "");  // 去除两端空格
    
        let editObj = document.createElement('div');
        let editData2 = '';
        editObj.innerHTML = editData;
        //console.log(editObj)
        let total = editObj.childNodes.length;
        for(let i = 0; i < total; i++){
            let childNode = editObj.childNodes[i];
            let innerHtml = childNode.innerHTML,
                innerText = childNode.innerText;
            if(innerText){
                if(innerText == '&nbsp;' || innerText == ''){
                    continue;
                }
                if(childNode.tagName.toLowerCase() == 'table'){
                    let rowLen = childNode.rows.length;
                    let tabHtm = '';
                    for(let x = 0; x < rowLen; x++){
                        let cellNum = childNode.rows[x].cells.length;
                        let cellHtm = '';
                        for(let j = 0; j < cellNum; j++){
                            let cellTxt = childNode.rows[x].cells[j].innerText;
                            cellTxt = cellTxt.replace(/(^\s*)|(\s*$)/g, "");
                            cellHtm += '<td>' + cellTxt + '</td>';
                        }
                        tabHtm += '<tr>' + cellHtm + '</tr>';
                    }
                    editData2 += '<div class=\'editTable\'>';
                    editData2 += '<table cellpadding=\'0\' cellspacing=\'0\' style=\'width: 100%;margin: 0 auto;\'>';
                    editData2 += '<tbody>' + tabHtm + '</tbody>';
                    editData2 += '</table>';
                    editData2 += '</div>';
                    continue;
                }
                //console.log(innerText);
                innerText = innerText.replace(/(^\s*)|(\s*$)/g, "");
                innerText = innerText.replace(/<\/?.+?>/g, "</p><p style=\'text-indent: 2em;\'>"); // 替换br标签
                innerText = innerText.replace(/[\r\n]/g, "</p><p style=\'text-indent: 2em;\'>"); // 替换换行符
                editData2 += '<p style=\'text-indent: 2em;\'>' + innerText + '</p>';
            }
        }
        editor.setData(editData2);
        editor.fire('saveSnapshot');
    }

    // 文本保留格式
    ckeditExtensionOb.textOld = (editor)=>{
        var editData = editor.getData();
        editData = editData.replace(/(^\s*)|(\s*$)/g, "");  // 去除两端空格

        var editObj = document.createElement('div');
        var editData2 = '';
        editObj.innerHTML = editData;
        var total = editObj.childNodes.length;
        for (var i = 0; i < total; i++) {
            var childNode = editObj.childNodes[i];
            var innerHtml = childNode.innerHTML,
                outerHtml = childNode.outerHTML;
            if (childNode.tagName!=undefined&&childNode.tagName.toLowerCase() == 'table') {
                editData2 += '<table cellpadding=\'0\' cellspacing=\'0\' style=\'width: 100%;margin: 0 auto;\'>' + innerHtml + '</table>';
            } else {
                if(childNode.tagName!=undefined)
                {
                    editData2 += outerHtml;
                }
            }
        }
        editor.setData(editData2);
        editor.fire('saveSnapshot');
    }
})();
